using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace SmartHRMS.Infrastructure
{
    public abstract class BusinessBase<T> : IEntity where T : IEntity
    {
        private List<BrokenRule> _brokenRules = new List<BrokenRule>();
        private Guid _ID = Guid.Empty;
		
        public System.Guid Id
        {
            get
            {
                return _ID;
            }
            private set
            {
                _ID = value;
            }
        }
		
        #region Equality Overrides
		
        public override bool Equals(object obj)
        {
            if (obj != null && obj.GetType() == typeof(T))
            {
                if (this.hasSameIdentifierAs((T)obj))
                {
                    // Both entities are persistant so we are able to
                    // compare the identifiers
                    return true;
                }
                else if (this.hasDifferentPersistanceContextAs((T)obj))
                {
                    // One entity is transient one is persistant so that cannot
                    // be eqaul
                    return false;
                }
            }
            return false;
        }

        public bool IsTransient()
        {
            return this.Id == Guid.Empty;
        }
		
        private bool hasSameIdentifierAs(T EntityToCompare)
        {
            if (!(IsTransient() && EntityToCompare.IsTransient()))
            {
                return this.Id == EntityToCompare.Id;
            }
            else
            {
                return false;
            }
        }
		
        public override int GetHashCode()
        {
            if (IsTransient())
            {
                return this.GetType().FullName.GetHashCode();
            }
            else
            {
                return string.Format("{0}{1}", this.GetType().FullName, Id.ToString()).GetHashCode();
            }
        }
		
        private bool hasDifferentPersistanceContextAs(T EntityToCompare)
        {
            return !(IsTransient() == EntityToCompare.IsTransient());
        }
		
        #endregion
		
        #region Entity Validation
		
        public abstract void validate();
		
        public List<BrokenRule> BrokenRules
        {
            get
            {
                return this._brokenRules;
            }
        }
		
        public ReadOnlyCollection<BrokenRule> GetBrokenRules()
        {
            this.BrokenRules.Clear();
            this.validate();
            return this._brokenRules.AsReadOnly();
        }

        #endregion

        #region IEntity Members

        Guid IEntity.Id
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        bool IEntity.IsTransient()
        {
            return this.Id == Guid.Empty;
        }
        #endregion
    }
}
